package io.bluefw.blueui.dsl.render.widget;

import java.util.List;
import java.util.Map;

import io.bluefw.blueui.dsl.Widget;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class GridWidget extends WidgetRender {
	public GridWidget(Widget widget) {
		super("grid.ftl");
		this.widget = widget;
		
		// 默认属性
		Map<String, Object> props = widget.getProps();///
		props.putIfAbsent("editable", false);
		props.putIfAbsent("pageable", true);
		props.putIfAbsent("sortable", true);
		props.putIfAbsent("showfilterrow", false);
		props.putIfAbsent("rowsheight", 29);
		props.putIfAbsent("pagerheight", 30);
		props.putIfAbsent("columnsheight", 30);
		props.putIfAbsent("autoheight", false);
		props.putIfAbsent("height", "100%");
		props.putIfAbsent("altrows", true);
		props.putIfAbsent("showfilterrow", false);
		props.putIfAbsent("filterable", false);
		props.putIfAbsent("pagesizeoptions", "['15', '30', '50']");
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public StringBuffer render() {
		List<Map<String, Object>> columns = (List<Map<String, Object>>) widget.getProps().get("columns");
		for (Map<String, Object> column : columns) {
			if (column.containsKey("binding")) {
				column.putIfAbsent("text", 
						widget.getEntity().getGlobalize(column.get("binding").toString()).value());
			}
			if (!column.containsKey("value")) {
				column.putIfAbsent("value", column.get("binding"));
			}
		}
		return freemarker.render(this, this.getTemplateName());
	}
}
